package org.jadestudio.dsdown.action;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jadestudio.dsdown.entity.ManageAdminLogin;
import org.jadestudio.dsdown.service.ManageLoginService;
import org.jadestudio.dsdown.util.SysInfo;
import org.springframework.stereotype.Component;

@Component(value="/jadestudio/portalLogin")
public class ManageLoginAction  extends DispatchAction {
	public ManageLoginAction(){
		
	}
	@Resource
	ManageLoginService manageLoginService;

	private Map<String,Long> memUsed;
	private Properties sysinfo;
	
	private void setMemandSysinfo(HttpServletRequest request){
		memUsed=SysInfo.getMemUsedState();
		sysinfo=SysInfo.getSysInfo();
		request.setAttribute("memused",memUsed);
		request.setAttribute("sysinfo",sysinfo);
	}
	/**
	 * 后台登录逻辑
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag>0){
			this.setMemandSysinfo(request);
			return mapping.findForward("success");//直接登录
		}else{
			String userName = request.getParameter("login_name")!=null?request.getParameter("login_name").toString():"";
			String userPass = request.getParameter("pass_word")!=null?request.getParameter("pass_word").toString():"";
			if(userName.equals("") || userPass.equals("")){
				return mapping.findForward("fail");
			}else{
				ManageAdminLogin manageAdminLogin =new ManageAdminLogin();
				manageAdminLogin.setLogin_name(userName);
				manageAdminLogin.setPass_word(userPass);
				int manageLoginFlag = manageLoginService.userLogin(manageAdminLogin,request);
				if(manageLoginFlag==1){
					this.setMemandSysinfo(request);
					return mapping.findForward("success");
				}else{
					return mapping.findForward("fail");
				}
			}
		}
	}
	
}
